package com.cheer.ba.view.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.model.QyeAuthModel;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.view.dialog.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 企业认证列表
 */
public class QyeAuthListAty extends BaseCommAty {

    @BindView(R.id.id_search_key)
    EditText mEditName;
    private String mSaveName;

    @BindView(R.id.id_qye_auth_listv)
    ListView mListView;
    private CommonAdapter<QyeAuthModel> mOilGoodAdp;
    private List<QyeAuthModel> mQyeAuths = new ArrayList<QyeAuthModel>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_qye_auth_list, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mQyeAuths.isEmpty();
    }

    @Override
    public void initView() {
        setTitleText("企业认证");
        mOilGoodAdp = new CommonAdapter<QyeAuthModel>(aty, mQyeAuths, R.layout.adp_qye_auth) {
            @Override
            public void convert(ViewHolder helper, final QyeAuthModel item) {
                if (null != item) {
                    ImageUtils.showRoundImg(aty, Protect.setPic(item.orgLogo), R.drawable.ic_bg_oil_sale, R.drawable.ic_bg_oil_sale, (ImageView) helper.getViewById(R.id.id_qye_logo_img));
                    helper.setText(R.id.id_qye_name_txt, item.orgNickName);
                    helper.setText(R.id.id_qye_type_txt, "(" + item.orgType + ")");
                    if (TextUtils.equals("1", item.auditStatus)) {
                        helper.getViewById(R.id.id_qye_auth_txt).setVisibility(View.VISIBLE);
                    } else {
                        helper.getViewById(R.id.id_qye_auth_txt).setVisibility(View.GONE);
                    }
                    helper.setText(R.id.id_qye_tel_txt, item.orgTel);
                    helper.getViewById(R.id.id_qye_auth_btn).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //去认证
                            UserAuthAty.startUserAuthAty(aty, item.uid, item.orgName, item.orgTel);
                        }
                    });
                    helper.getViewById(R.id.id_qye_tel_txt).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (StringUtils.isEmpty(item.orgTel)) {
                                return;
                            }
                            //拨打电话
                            DialogUtils.showCommDialog(aty, "联系电话：" + item.orgTel, "取消", "拨打", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtils.DialogDiss();
                                    StringUtils.telPhone(aty, item.orgTel);
                                }
                            });
                            DialogUtils.DialogShow();
                        }
                    });
                }
            }
        };
        mListView.setAdapter(mOilGoodAdp);
    }

    @Override
    public void initData() {
    }

    @OnClick(R.id.id_search_btn)
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_search_btn:
                mSaveName = mEditName.getText().toString().trim();
                if (StringUtils.isEmpty(mSaveName)) {
                    ToastUtil.showToast("请输入搜索关键字");
                } else {
                    ((FindOilPresenter) presenter).getQyeAuthList(mSaveName);
                }
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_ORG_LIST)) {
                List<QyeAuthModel> mOils = (List<QyeAuthModel>) t;
                if (!mOils.isEmpty()) {
                    mQyeAuths.clear();
                    mQyeAuths.addAll(mOils);
                    mOilGoodAdp.notifyDataSetChanged();
                }
            }
        }
    }
}
